$(function(){
    var timeout = 3000;
    var timer;
    var first = true;

    $(document).ready(function() {
        timer = $.timer(timeout, function() {
                loadData();
                loadCT();
                checkStatus();
        });
    });

    function checkStatus(){
        $.ajax({
            type: "GET",
            url: "../ajax/checkStatus",
            data: {
                p_id:$("input#p_id").val(),
                c_id:$("input#c_id").val()
            },
            dataType: "html",
            success: function(data) {
                if(data=="false"){
                    alert("สิ้นสุดการเชื่อมต่อ");
                    parent.window.open("/TeleStroke/doctor/listPatient",'_self');
                }
            }
        });
    }

    function loadCT(){
        $.ajax({
            type: "POST",
            url: "../ajax/ctscan",
            data: {
                p_id:$("input#p_id").val(),
                c_id:$("input#c_id").val()
            },
            dataType: "html",
            success: function(data) {
                $("#CTScanFile").html(data);
            }
        });
    }

    function calStrokeTime(strokeTimeHour,strokeTimeMinute){        
        hh = strokeTimeHour
        mm = strokeTimeMinute

        hh = Number(hh) + Number(4);
        mm = Number(mm) + 30;
        if(mm > 60){
            mm = Number(mm) - 60;
            hh += 1;
        }

        var d = new Date();

        ch = d.getHours();
        cm = d.getMinutes();
        cs = d.getSeconds();

        if(mm<cm){
            hh = Number(hh)-1;
            mm = Number(mm) + 60;
        }

        lh = Number(hh) - Number(ch);
        lm = Number(mm) - Number(cm);
        ss = Number(60) - Number(cs);

        if(lh<0){
            lh = 0;
            lm = 0;
            ss = 0;
        }

        $("div#strokeLeftHour").html(lh);
        $("div#strokeLeftMinute").html(lm);
        $("div#strokeLeftSecond").html(ss);
    }

    function loadData(){
        $.ajax({
            type: "POST",
            url: "../ajax/loadPatientData",
            data: {                
                p_id:$("input#p_id").val(),
                c_id:$("input#c_id").val()
            },
            dataType: "json",
            success: function(data) {
                var total = 0;
                $.each(data, function(key, val) {                    
                    if(key=="no1a"){
                        if(val=="0"){
                            $("#no1a").html("Alert (0)");
                        }else if(val=="1"){
                            $("#no1a").html("Drowsy (1)");
                        }else if(val=="2"){
                            $("#no1a").html("Stuporous (2)");
                        }else if(val=="3"){
                            $("#no1a").html("Coma (3)");
                        }
                        total +=Number(val);
                    }
                    else if(key=="no1b"){
                        if(val=="0"){
                            $("div#no1b").html("Both correct (0)");
                        }else if(val=="1"){
                            $("div#no1b").html("One correct (1)");
                        }else if(val=="2"){
                            $("div#no1b").html("None correct (2)");
                        }
                        total +=Number(val);
                    }
                    else if(key=="no1c"){
                        if(val=="0"){
                            $("div#no1c").html("Obeys both (0)");
                        }else if(val=="1"){
                            $("div#no1c").html("Obeys none (1)");
                        }
                        total +=Number(val);
                    }else if(key=="no2"){
                        if(val=="0"){
                            $("div#no2").html("Normal (0)");
                        }else if(val=="1"){
                            $("div#no2").html("Partial gaze palsy (1)");
                        }else if(val=="2"){
                            $("div#no2").html("Forced deviation (2)");
                        }
                        total +=Number(val);
                    }else if(key=="no3"){
                        if(val=="0"){
                            $("div#no3").html("No visual loss (0)");
                        }else if(val=="1"){
                            $("div#no3").html("Partial hemianopia (1)");
                        }else if(val=="2"){
                            $("div#no3").html("Complete  hemianopia (2)");
                        }else if(val=="3"){
                            $("div#no3").html("Bilateral hemianopia (3)");
                        }
                        total +=Number(val);
                    }else if(key=="no4"){
                        if(val=="0"){
                            $("div#no4").html("Normal (0)");
                        }else if(val=="1"){
                            $("div#no4").html("Minor (1)");
                        }else if(val=="2"){
                            $("div#no4").html("Partial (2)");
                        }else if(val=="3"){
                            $("div#no4").html("Complete (3)");
                        }
                        total +=Number(val);
                    }else if(key=="no5a"){
                        if(val=="0"){
                            $("div#no5a").html("No drift (0)");
                        }else if(val=="1"){
                            $("div#no5a").html("Drift (1)");
                        }else if(val=="2"){
                            $("div#no5a").html("Fall in 10 secs (2)");
                        }else if(val=="3"){
                            $("div#no5a").html("No effort against gravity (3)");
                        }else if(val=="4"){
                            $("div#no5a").html("No movement (4)");
                        }
                        total +=Number(val);
                    }else if(key=="no5b"){
                        if(val=="0"){
                            $("div#no5b").html("No drift (0)");
                        }else if(val=="1"){
                            $("div#no5b").html("Drift (1)");
                        }else if(val=="2"){
                            $("div#no5b").html("Fall in 10 secs (2)");
                        }else if(val=="3"){
                            $("div#no5b").html("No effort against gravity (3)");
                        }else if(val=="4"){
                            $("div#no5b").html("No movement (4)");
                        }
                        total +=Number(val);
                    }else if(key=="no6a"){
                        if(val=="0"){
                            $("div#no6a").html("No drift (0)");
                        }else if(val=="1"){
                            $("div#no6a").html("Drift (1)");
                        }else if(val=="2"){
                            $("div#no6a").html("Fall in 10 secs (2)");
                        }else if(val=="3"){
                            $("div#no6a").html("No effort against gravity (3)");
                        }else if(val=="4"){
                            $("div#no6a").html("No movement (4)");
                        }
                        total +=Number(val);
                    }else if(key=="no6b"){
                        if(val=="0"){
                            $("div#no6b").html("No drift (0)");
                        }else if(val=="1"){
                            $("div#no6b").html("Drift (1)");
                        }else if(val=="2"){
                            $("div#no6b").html("Fall in 10 secs (2)");
                        }else if(val=="3"){
                            $("div#no6b").html("No effort against gravity (3)");
                        }else if(val=="4"){
                            $("div#no6b").html("No movement (4)");
                        }
                        total +=Number(val);
                    }else if(key=="no7"){
                        if(val=="0"){
                            $("div#no7").html("Absent (0)");
                        }else if(val=="1"){
                            $("div#no7").html("Upper or lower limb (1)");
                        }else if(val=="2"){
                            $("div#no7").html("Upper & lower limb (2)");
                        }
                        total +=Number(val);
                    }else if(key=="no8"){
                        if(val=="0"){
                            $("div#no8").html("Normal (0)");
                        }else if(val=="1"){
                            $("div#no8").html("Partial loss (1)");
                        }else if(val=="2"){
                            $("div#no8").html("Dense loss (2)");
                        }
                        total +=Number(val);
                    }else if(key=="no9"){
                        if(val=="0"){
                            $("div#no9").html("No aphasia (0)");
                        }else if(val=="1"){
                            $("div#no9").html("Mild to moderate (1)");
                        }else if(val=="2"){
                            $("div#no9").html("Severe (2)");
                        }else if(val=="3"){
                            $("div#no9").html("Mute (3)");
                        }
                        total +=Number(val);
                    }else if(key=="no10"){
                        if(val=="0"){
                            $("div#no10").html("Normal articulation (0)");
                        }else if(val=="1"){
                            $("div#no10").html("Mild to moderate (1)");
                        }else if(val=="2"){
                            $("div#no10").html("Severe (2)");
                        }
                        total +=Number(val);
                    }else if(key=="no11"){
                        if(val=="0"){
                            $("div#no11").html("No neglect (0)");
                        }else if(val=="1"){
                            $("div#no11").html("Sensory or visual (1)");
                        }else if(val=="2"){
                            $("div#no11").html("Sensory & visual (2)");
                        }
                        total +=Number(val);
                    }
                    //Stroke Data
                    else if(key=="strokeTimeYN"){
                        if(val=="yes"){
                            $("#strokeTime").html(
                            "ทราบ เวลา " + data['strokeTimeHour']+" : "+ data['strokeTimeMinute']+" วันที่: " + data['strokeTimeDMY']
                            );
                            calStrokeTime(data['strokeTimeHour'],data['strokeTimeMinute']);
                        }else if(val=="no"){
                             $("#strokeTime").html("ไม่ทราบ");
                        }
                    }
                    else if(key=="strokeNormalTimeYN"){
                        if(val=="yes"){
                            $("#strokeNormalTime").html(
                            "ทราบ เวลา " + data['strokeNormalTimeHour']+" : "+ data['strokeNormalTimeMinute']+" วันที่: " + data['strokeNormalTimeDMY']
                            );
                        }else if(val=="no"){
                             $("#strokeNormalTime").html("ไม่ทราบ");
                        }
                    }

                    else if(key=="cc"){$("#cc").html(val);}
                    else if(key=="PI"){$("#PI").html(val);}
                    else if(key=="ph_ht_yn"){
                        if(val=="true")
                        {
                            $("#ph_ht_yn").attr('checked', true);
                            $("#ph_ht_yn").css('background-color','green');
                        }else
                        {
                            $("#ph_ht_yn").attr('checked', false);
                            $("#ph_ht_yn").css('background-color','red');
                        }
                    }
                    else if(key=="ph_ht_yrs"){$("#ph_ht_yrs").html(val);}
                    else if(key=="ph_ht_med"){$("#ph_ht_med").html(val);}
                    else if(key=="ph_dm_yn"){
                        if(val=="true")
                        {
                            $("#ph_dm_yn").attr('checked', true);
                            $("#ph_dm_yn").css('background-color','green');
                        }else
                        {
                            $("#ph_dm_yn").attr('checked', false);
                            $("#ph_dm_yn").css('background-color','red');
                        }
                    }
                    else if(key=="ph_dm_med"){$("#ph_dm_med").html(val);}
                    else if(key=="ph_dyslipidemia_yn"){
                        if(val=="true"){
                            $("#ph_dyslipidemia_yn").attr('checked', true);
                            $("#ph_dyslipidemia_yn").css('background-color','green');
                        }else{
                            $("#ph_dyslipidemia_yn").attr('checked', false);
                            $("#ph_dyslipidemia_yn").css('background-color','red');
                        }
                    }
                    else if(key=="ph_dyslipidemia_yrs"){$("#ph_dyslipidemia_yrs").html(val);}
                    else if(key=="ph_dyslipidemia_med"){$("#ph_dyslipidemia_med").html(val);}
                    else if(key=="ph_smoking_yn"){
                        if(val=="true"){
                            $("#ph_smoking_yn").attr('checked', true);
                            $("#ph_smoking_yn").css('background-color','green');
                        }else{
                            $("#ph_smoking_yn").attr('checked', false);
                            $("#ph_smoking_yn").css('background-color','red');
                        }
                    }
                    else if(key=="ph_smoking_pack"){$("#ph_smoking_pack").html(val);}
                    else if(key=="ph_smoking_yrs"){$("#ph_smoking_yrs").html(val);}
                    else if(key=="ph_smoking_qyrs"){$("#ph_smoking_qyrs").html(val);}
                    else if(key=="ph_heart_disease_yn"){
                        if(val=="true"){
                            $("#ph_heart_disease_yn").attr('checked', true);
                            $("#ph_heart_disease_yn").css('background-color','green');
                        }else{
                            $("#ph_heart_disease_yn").attr('checked', false);
                            $("#ph_heart_disease_yn").css('background-color','red');
                        }
                    }
                    else if(key=="ph_heart_disease_type"){$("#ph_heart_disease_type").html(val);}
                    else if(key=="ph_heart_disease_med"){$("#ph_heart_disease_med").html(val);}
                    else if(key=="ph_stroke_yn"){
                        if(val=="true"){
                            $("#ph_stroke_yn").attr('checked', true);
                            $("#ph_stroke_yn").css('background-color','green');
                        }else{
                            $("#ph_stroke_yn").attr('checked', false);
                            $("#ph_stroke_yn").css('background-color','red');
                        }
                    }
                    else if(key=="ph_stroke_year"){$("#ph_stroke_year").html(val);}
                    else if(key=="ph_stroke_type"){
                        if(val=="i"){
                            $("#ph_stroke_type").html("is chemic");
                        }
                        else if(val=="ich"){
                             $("#ph_stroke_type").html("ICH");
                        }
                        else if(val=="SAH"){
                             $("#ph_stroke_type").html("sah");
                        }else{
                             $("#ph_stroke_type").html("-");
                        }
                    }else if(key=="ph_others_yn"){
                        if(val=="true"){
                            $("#ph_others_yn").attr('checked', true);
                            $("#ph_others_yn").css('background-color','green');
                        }else{
                            $("#ph_others_yn").attr('checked', false);
                            $("#ph_others_yn").css('background-color','red');
                        }
                    }
                    else if(key=="ph_others"){$("#ph_others").html(val);}

                    //Stroke Phy Data
                    else if(key=="BPmax"){$("#BPmax").html(val);}
                    else if(key=="BPmin"){$("#BPmin").html(val);}
                    else if(key=="PR"){$("#PR").html(val);}
                    else if(key=="PRminYN"){
                        if(val=="y"){
                            $("#PRminYN").html("<font color=blue>Yes</font>");
                        }
                        else if(val=="n"){
                             $("#PRminYN").html("<font color=red>No</font>");
                        }
                    }
                    else if(key=="CarotidLt"){
                        if(val=="y"){
                            $("#CarotidLt").html("<font color=blue>Yes</font>");
                        }
                        else if(val=="n"){
                             $("#CarotidLt").html("<font color=red>No</font>");
                        }
                    }
                    else if(key=="CarotidRt"){
                        if(val=="y"){
                            $("#CarotidRt").html("<font color=blue>Yes</font>");
                        }
                        else if(val=="n"){
                             $("#CarotidRt").html("<font color=red>No</font>");
                        }
                    }
                    else if(key=="HeartNA"){
                        if(val=="n"){
                            $("#HeartNA").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#HeartNA").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="Heart"){$("#Heart").html(val);}
                    else if(key=="ConsciousnessE"){$("#ConsciousnessE").html(val);}
                    else if(key=="ConsciousnessM"){$("#ConsciousnessM").html(val);}
                    else if(key=="ConsciousnessV"){$("#ConsciousnessV").html(val);}
                    else if(key=="OrientationTime"){$("#OrientationTime").html(val);}
                    else if(key=="OrientationPlace"){$("#OrientationPlace").html(val);}
                    else if(key=="OrientationPerson"){$("#OrientationPerson").html(val);}
                    else if(key=="Speech"){
                        if(val=="d"){
                            $("#Speech").html("dysarthric");
                        }
                        else if(val=="a"){
                             $("#Speech").html("aphasic");
                        }
                        else if(val=="n"){
                             $("#Speech").html("normal");
                        }
                    }
                    else if(key=="Aphasia"){
                        if(val=="m"){
                            $("#Aphasia").html("motor");
                        }
                        else if(val=="s"){
                             $("#Aphasia").html("sensory");
                        }
                        else if(val=="g"){
                             $("#Aphasia").html("global");
                        }
                    }
                    else if(key=="Fluency"){
                        if(val=="n"){
                            $("#Fluency").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#Fluency").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="Comprehension"){
                        if(val=="n"){
                            $("#Comprehension").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#Comprehension").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="Paraphasia"){
                        if(val=="n"){
                            $("#Paraphasia").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#Paraphasia").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="Repetition"){
                        if(val=="n"){
                            $("#Repetition").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#Repetition").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="Naming"){
                        if(val=="n"){
                            $("#Naming").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#Naming").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="CranialL"){$("#CranialL").html(val);}
                    else if(key=="CranialR"){$("#CranialR").html(val);}
                    else if(key=="Horner"){$("#Horner").html(val);}
                    else if(key=="HornerLR"){
                        if(val=="l")
                            $("#HornerLR").html("Lt");
                        else if(val=="r")
                            $("#HornerLR").html("Rt");
                    }
                    else if(key=="PupilsLt"){$("#PupilsLt").html(val);}
                    else if(key=="PupilsLtc"){                        
                        if(val=="g"){
                            $("#PupilsLtc").html("good");
                        }
                        else if(val=="s"){
                             $("#PupilsLtc").html("sluggish");
                        }
                        else if(val=="n"){
                             $("#PupilsLtc").html("not reactive to light");
                        }
                    }
                    else if(key=="PupilsRt"){$("#PupilsRt").html(val);}
                    else if(key=="PupilsRtc"){
                        if(val=="g"){
                            $("#PupilsRtc").html("good");
                        }
                        else if(val=="s"){
                             $("#PupilsRtc").html("sluggish");
                        }
                        else if(val=="n"){
                             $("#PupilsRtc").html("not reactive to light");
                        }
                    }
                    else if(key=="VALt"){$("#VALt").html(val);}
                    else if(key=="VARt"){$("#VARt").html(val);}
                    else if(key=="VFLt"){$("#VFLt").html(val);}
                    else if(key=="VFRt"){$("#VFRt").html(val);}
                    else if(key=="FacialWeaknessLR"){
                        if(val=="l"){
                            $("#FacialWeaknessLR").html("Lt");
                        }
                        else if(val=="r"){
                             $("#FacialWeaknessLR").html("Rt");
                        }
                    }
                    else if(key=="FacialWeaknessUL"){
                        if(val=="u"){
                            $("#FacialWeaknessUL").html("UMN");
                        }
                        else if(val=="l"){
                             $("#FacialWeaknessUL").html("LMN");
                        }
                    }
                    else if(key=="TongueDeviationYN"){
                        if(val=="y"){
                            $("#TongueDeviationYN").html("Y");
                            if(data['TongueDeviationLR']=="l"){
                                $("#TongueDeviationLR").html("To (Lt)");
                            }else if(data['TongueDeviationLR']=="r"){
                                $("#TongueDeviationLR").html("To (Rt)");
                            }
                        }
                        else if(val=="n"){
                             $("#TongueDeviationYN").html("N");
                             $("#TongueDeviationLR").html("");
                        }
                    }
                    else if(key=="GagReflexL"){$("#GagReflexL").html(val);}
                    else if(key=="GagReflexR"){$("#GagReflexR").html(val);}
                    else if(key=="UvalaMD"){
                        if(val=="d"){
                            $("#UvalaMD").html("deviate to");
                            if(data['UvalaLR']=="l"){
                                $("#UvalaLR").html("Lt");
                            }else if(data['UvalaLR']=="r"){
                                $("#UvalaLR").html("Rt");
                            }
                        }
                        else if(val=="m"){
                             $("#UvalaMD").html("midline");
                             $("#UvalaLR").html("");
                        }
                    }
                    else if(key=="MotorLTp"){$("#MotorLTp").html(val);}
                    else if(key=="MotorLTd"){$("#MotorLTd").html(val);}
                    else if(key=="MotorRTp"){$("#MotorRTp").html(val);}
                    else if(key=="MotorRTd"){$("#MotorRTd").html(val);}
                    else if(key=="MotorLBp"){$("#MotorLBp").html(val);}
                    else if(key=="MotorLBd"){$("#MotorLBd").html(val);}
                    else if(key=="MotorRBp"){$("#MotorRBp").html(val);}
                    else if(key=="MotorRBd"){$("#MotorRBd").html(val);}
                    else if(key=="PronatorDriftingLR"){
                        if(val=="l"){
                            $("#PronatorDriftingLR").html("Lt");
                        }
                        else if(val=="r"){
                             $("#PronatorDriftingLR").html("Rt");
                        }
                    }
                    else if(key=="CerebellarYN"){
                        if(val=="y"){
                            $("#CerebellarYN").html("Y");
                        }
                        else if(val=="n"){
                             $("#CerebellarYN").html("N");
                        }
                    }
                    else if(key=="CerebellarGLR"){
                        if(val=="l"){
                            $("#CerebellarGLR").html("Lt");
                        }
                        else if(val=="r"){
                             $("#CerebellarGLR").html("Rt");
                        }
                    }
                    else if(key=="CerebellarTYN"){
                        if(val=="y"){
                            $("#CerebellarTYN").html("Y");
                        }
                        else if(val=="n"){
                             $("#CerebellarTYN").html("N");
                        }
                    }
                    else if(key=="CerebellarFYN"){
                        if(val=="y"){
                            $("#CerebellarFYN").html("Y");
                        }
                        else if(val=="n"){
                             $("#CerebellarFYN").html("N");
                        }
                    }
                    else if(key=="SensoryTMD"){
                        if(val=="n"){
                            $("#SensoryTMD").html("Normal");
                            $("#SensoryHlr").html("");
                            $("#SensoryPlr").html("");
                            $("#SensoryP").html("");
                        }
                        else if(val=="h"){
                             $("#SensoryTMD").html("Hemianesthenia");
                             if(data['SensoryHlr']=="l"){
                                 $("#SensoryHlr").html("Lt");
                             }
                             else if(data['SensoryHlr']=="r"){
                                 $("#SensoryHlr").html("Rt");
                             }
                            $("#SensoryPlr").html("");
                            $("#SensoryP").html("");
                        }
                        else if(val=="p"){
                             $("#SensoryTMD").html("Parhal hemisensory loss");
                             if(data['SensoryPlr']=="l"){
                                 $("#SensoryPlr").html("Lt");
                             }
                             else if(data['SensoryPlr']=="r"){
                                 $("#SensoryPlr").html("Rt");
                             }
                             $("#SensoryP").html(data['SensoryP']);
                             $("#SensoryHlr").html("");
                        }
                    }
                    else if(key=="NeglectLt"){
                        if(val=="n"){
                            $("#NeglectLt").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#NeglectLt").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="NeglectRt"){
                        if(val=="n"){
                            $("#NeglectRt").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#NeglectRt").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="ProprioceptionLt"){
                        if(val=="n"){
                            $("#ProprioceptionLt").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#ProprioceptionLt").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="ProprioceptionRt"){
                        if(val=="n"){
                            $("#ProprioceptionRt").html("<font color=blue>Normal</font>");
                        }
                        else if(val=="a"){
                             $("#ProprioceptionRt").html("<font color=red>Abnormal</font>");
                        }
                    }
                    else if(key=="FinalDXHI"){
                        if(val=="i"){
                            $("#FinalDXHI").html("<b>Ischemic stroke </b>");
                            $("#FinalDXHType").html("");
                        }
                        else if(val=="h"){
                             $("#FinalDXHI").html("<b>Hemorrhagic stroke </b>");
                             $("#FinalDXHType").html(data['FinalDXHType']);
                        }
                    }
                    else if(key=="VesselAP"){
                        if(val=="a"){
                            $("#VesselAP").html("Anterior circulation :");
                            if(data['VesselAc']=='a'){
                                $("#VesselAc").html("ACA");
                            }else if(data['VesselAc']=='m'){
                                $("#VesselAc").html("MCA");
                            }
                            $("#VesselPc").html("");
                        }
                        else if(val=="p"){
                             $("#VesselAP").html("Posterior circulation:");
                             if(data['VesselPc']=='pc'){
                                $("#VesselPc").html("PCA");
                            }else if(data['VesselPc']=='vb'){
                                $("#VesselPc").html("VB");
                            }else if(data['VesselPc']=='pi'){
                                $("#VesselPc").html("PICA");
                            }else if(data['VesselPc']=='ai'){
                                $("#VesselPc").html("AICA");
                            }else if(data['VesselPc']=='ce'){
                                $("#VesselPc").html("Cerebellar");
                            }
                            $("#VesselAc").html("");
                        }
                    }
                    else if(key=="Cause"){
                        if(val=="e"){
                            $("#Cause").html("<b>Emboli :</b>");
                            if(data['CauseE']=="c"){
                                $("#CauseE").html("cardiac");
                            }else if(data['CauseE']=="a"){
                                $("#CauseE").html("artery");
                            }if(data['CauseE']=="u"){
                                $("#CauseE").html("unknown");
                            }

                            $("#CauseLType").html("");
                        }
                        else if(val=="t"){
                             $("#Cause").html("<b>Thrombosis</b>");
                             $("#CauseLType").html("");
                             $("#CauseE").html("");
                        }
                        else if(val=="l"){
                             $("#Cause").html("<b>Lacunar stroke </b>: type");
                             if(data['CauseLType']=="pm"){
                                $("#CauseLType").html("Pure motor syndrome");
                            }else if(data['CauseLType']=="ps"){
                                $("#CauseLType").html("Pure sensory syndrome");
                            }else if(data['CauseLType']=="ah"){
                                $("#CauseLType").html("Ataxic hemiparesis");
                            }else if(data['CauseLType']=="dc"){
                                $("#CauseLType").html("Dysarthria clumsy hand syndrome");
                            }else if(data['CauseLType']=="sm"){
                                $("#CauseLType").html("Sensori-motor syndrome");
                            }
                            $("#CauseE").html("");
                        }
                        else if(val=="o"){
                             $("#Cause").html("<b>Others</b>");
                             $("#CauseOther").html(data['CauseOther']);
                             $("#CauseE").html("");
                             $("#CauseLType").html("");
                        }
                    }
                    else if(key=="Stage"){
                        if(val=="s"){
                            $("#Stage").html("Stable");
                        }else if(val=="p"){
                             $("#Stage").html("Progressive");
                        }else if(val=="c"){
                             $("#Stage").html("Completed");
                        }else if(val=="i"){
                             $("#Stage").html("Improved");
                        }else if(val=="t"){
                             $("#Stage").html("TIA");
                        }
                    }
                    else if(key=="stp_others"){$("#stp_others").html(val);}
                    else if(key=="ProvisionalDiagnosis"){$("#ProvisionalDiagnosis").html(val);}

                    //Lab
                    else if(key=="CTscanYN"){
                        if(val=="true"){
                            $('input[name=CTscanYN]').attr('checked', true);
                            $('input[name=CTscanYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=CTscanYN]').attr('checked', false);
                            $('input[name=CTscanYN]').css('background-color','red');
                        }
                    }
                    else if(key=="CTNoICHYN"){
                        if(val=="true"){
                            $('input[name=CTNoICHYN]').attr('checked', true);
                            $('input[name=CTNoICHYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=CTNoICHYN]').attr('checked', false);
                            $('input[name=CTNoICHYN]').css('background-color','red');
                        }
                    }
                    else if(key=="CTnobrainYN"){
                        if(val=="true"){
                            $('input[name=CTnobrainYN]').attr('checked', true);
                            $('input[name=CTnobrainYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=CTnobrainYN]').attr('checked', false);
                            $('input[name=CTnobrainYN]').css('background-color','red');
                        }
                    }
                    else if(key=="CTPerfusionYN"){
                        if(val=="true"){
                            $('input[name=CTPerfusionYN]').attr('checked', true);
                            $('input[name=CTPerfusionYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=CTPerfusionYN]').attr('checked', false);
                            $('input[name=CTPerfusionYN]').css('background-color','red');
                        }
                    }
                    else if(key=="MRIYN"){
                        if(val=="true"){
                            $('input[name=MRIYN]').attr('checked', true);
                            $('input[name=MRIYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=MRIYN]').attr('checked', false);
                            $('input[name=MRIYN]').css('background-color','red');
                        }
                    }
                    else if(key=="MRIResult"){$("#MRIResult").html(val);}
                    else if(key=="MRINoICHYN"){
                        if(val=="true"){
                            $('input[name=MRINoICHYN]').attr('checked', true);
                            $('input[name=MRINoICHYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=MRINoICHYN]').attr('checked', false);
                            $('input[name=MRINoICHYN]').css('background-color','red');
                        }
                    }
                    else if(key=="MRInobrainYN"){
                        if(val=="true"){
                            $('input[name=MRInobrainYN]').attr('checked', true);
                            $('input[name=MRInobrainYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=MRInobrainYN]').attr('checked', false);
                            $('input[name=MRInobrainYN]').css('background-color','red');
                        }
                    }
                    else if(key=="MRIDWI30YN"){
                        if(val=="true"){
                            $('input[name=MRIDWI30YN]').attr('checked', true);
                            $('input[name=MRIDWI30YN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=MRIDWI30YN]').attr('checked', false);
                            $('input[name=MRIDWI30YN]').css('background-color','red');
                        }
                    }
                    else if(key=="MRIDWIPWIYN"){
                        if(val=="true"){
                            $('input[name=MRIDWIPWIYN]').attr('checked', true);
                            $('input[name=MRIDWIPWIYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=MRIDWIPWIYN]').attr('checked', false);
                            $('input[name=MRIDWIPWIYN]').css('background-color','red');
                        }
                    }
                    else if(key=="MRAYN"){
                        if(val=="true"){
                            $('input[name=MRAYN]').attr('checked', true);
                            $('input[name=MRAYN]').css('background-color','Green');
                        }else if(val=="false"){
                            $('input[name=MRAYN]').attr('checked', false);
                            $('input[name=MRAYN]').css('background-color','red');
                        }
                    }
                    else if(key=="MRAMCAYN"){
                        if(val=="true"){
                            $('input[name=MRAMCAYN]').attr('checked', true);
                            $('input[name=MRAMCAYN]').css('background-color','Green');
                            if(data['MRAMCAso']=="s"){
                                $('#MRAMCAso').html("(Stenosis)");
                            }else if(data['MRAMCAso']=="o"){
                                $('#MRAMCAso').html("(Occlusion)");
                            }
                        }else if(val=="false"){
                            $('input[name=MRAMCAYN]').attr('checked', false);
                            $('input[name=MRAMCAYN]').css('background-color','red');
                             $('#MRAMCAso').html("");
                        }
                    }
                    else if(key=="MRAACAYN"){
                        if(val=="true"){
                            $('input[name=MRAACAYN]').attr('checked', true);
                            $('input[name=MRAACAYN]').css('background-color','Green');
                            if(data['MRAACAso']=="s"){
                                $('#MRAACAso').html("(Stenosis)");
                            }else if(data['MRAACAso']=="o"){
                                $('#MRAACAso').html("(Occlusion)");
                            }
                        }else if(val=="false"){
                            $('input[name=MRAACAYN]').attr('checked', false);
                            $('input[name=MRAACAYN]').css('background-color','red');
                             $('#MRAACAso').html("");
                        }
                    }
                    else if(key=="MRABAYN"){
                        if(val=="true"){
                            $('input[name=MRABAYN]').attr('checked', true);
                            $('input[name=MRABAYN]').css('background-color','Green');
                            if(data['MRABAso']=="s"){
                                $('#MRABAso').html("(Stenosis)");
                            }else if(data['MRABAso']=="o"){
                                $('#MRABAso').html("(Occlusion)");
                            }
                        }else if(val=="false"){
                            $('input[name=MRABAYN]').attr('checked', false);
                            $('input[name=MRABAYN]').css('background-color','red');
                            $('#MRABAso').html("");
                        }
                    }
                    else if(key=="MRAICAYN"){
                        if(val=="true"){
                            $('input[name=MRAICAYN]').attr('checked', true);
                            $('input[name=MRAICAYN]').css('background-color','Green');
                            if(data['MRAICAso']=="s"){
                                $('#MRAICAso').html("(Stenosis)");
                            }else if(data['MRAICAso']=="o"){
                                $('#MRAICAso').html("(Occlusion)");
                            }
                        }else if(val=="false"){
                            $('input[name=MRAICAYN]').attr('checked', false);
                            $('input[name=MRAICAYN]').css('background-color','red');
                            $('#MRAICAso').html("");
                        }
                    }
                    else if(key=="MRAotherYN"){
                        if(val=="true"){
                            $('input[name=MRAotherYN]').attr('checked', true);
                            $('input[name=MRAotherYN]').css('background-color','Green');
                            $('#MRAother').html(data['MRAother']);
                        }else if(val=="false"){
                            $('input[name=MRAotherYN]').attr('checked', false);
                            $('input[name=MRAotherYN]').css('background-color','red');
                            $('#MRAother').html("");
                        }
                    }
                    else if(key=="BloodPG"){
                        pg = Number(data['BloodPG']);
                        if(pg>50 && pg<400){
                            $('input[name=BloodPGYN]').attr('checked', true);                            
                        }else{
                            $('input[name=BloodPGYN]').attr('checked', false);
                            $('#BloodPG').css('color','red');
                        }                        
                        $('#BloodPG').html(val);
                    }
                    else if(key=="BloodPT"){$('#BloodPT').html(val);}
                    else if(key=="BloodPTT"){$('#BloodPTT').html(val);}
                    else if(key=="BloodPLT"){ 
                        plt = Number(data['BloodPLT']);
                        if(plt>100000){
                            $('input[name=BloodPLTYN]').attr('checked', true);
                            $('#BloodPLT').css('color','blue');
                        }else{
                            $('input[name=BloodPLTYN]').attr('checked', false);
                            $('#BloodPLT').css('color','red');
                        }
                        $('#BloodPLT').html(val);
                   }
                   else if(key=="BloodPGYN"){
                        if(val=="true"){
                            $('input[name=BloodPGYN]').attr('checked', true);
                            $('input[name=BloodPGYN]').css('background-color','Green');

                        }else if(val=="false"){
                            $('input[name=BloodPGYN]').attr('checked', false);
                            $('input[name=BloodPGYN]').css('background-color','red');
                        }
                    }
                    else if(key=="BloodPLTYN"){
                        if(val=="true"){
                            $('input[name=BloodPLTYN]').attr('checked', true);
                            $('input[name=BloodPLTYN]').css('background-color','Green');

                        }else if(val=="false"){
                            $('input[name=BloodPLTYN]').attr('checked', false);
                            $('input[name=BloodPLTYN]').css('background-color','red');
                        }
                    }
                    else if(key=="BloodINRYN"){
                        if(val=="true"){
                            $('input[name=BloodINRYN]').attr('checked', true);
                            $('input[name=BloodINRYN]').css('background-color','Green');

                        }else if(val=="false"){
                            $('input[name=BloodINRYN]').attr('checked', false);
                            $('input[name=BloodINRYN]').css('background-color','red');
                        }
                    }

                    //Estimate Data
                    else if(key=="estimate_i1"){
                        if(val=="true"){
                            $('input[name=estimate_i1]').attr('checked', true);
                            $('input[name=estimate_i1]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_i1]').attr('checked', false);
                            $('input[name=estimate_i1]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_i2"){
                        if(val=="true"){
                            $('input[name=estimate_i2]').attr('checked', true);
                            $('input[name=estimate_i2]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_i2]').attr('checked', false);
                            $('input[name=estimate_i2]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_i3"){
                        if(val=="true"){
                            $('input[name=estimate_i3]').attr('checked', true);
                            $('input[name=estimate_i3]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_i3]').attr('checked', false);
                            $('input[name=estimate_i3]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d1"){
                        if(val=="true"){
                            $('input[name=estimate_d1]').attr('checked', true);
                            $('input[name=estimate_d1]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d1]').attr('checked', false);
                            $('input[name=estimate_d1]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d2"){
                        if(val=="true"){
                            $('input[name=estimate_d2]').attr('checked', true);
                            $('input[name=estimate_d2]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d2]').attr('checked', false);
                            $('input[name=estimate_d2]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d3"){
                        if(val=="true"){
                            $('input[name=estimate_d3]').attr('checked', true);
                            $('input[name=estimate_d3]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d3]').attr('checked', false);
                            $('input[name=estimate_d3]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d4"){
                        if(val=="true"){
                            $('input[name=estimate_d4]').attr('checked', true);
                            $('input[name=estimate_d4]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d4]').attr('checked', false);
                            $('input[name=estimate_d4]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d5"){
                        if(val=="true"){
                            $('input[name=estimate_d5]').attr('checked', true);
                            $('input[name=estimate_d5]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d51]').attr('checked', false);
                            $('input[name=estimate_d5]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d6"){
                        if(val=="true"){
                            $('input[name=estimate_d6]').attr('checked', true);
                            $('input[name=estimate_d6]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d6]').attr('checked', false);
                            $('input[name=estimate_d6]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d7"){
                        if(val=="true"){
                            $('input[name=estimate_d7]').attr('checked', true);
                            $('input[name=estimate_d7]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d7]').attr('checked', false);
                            $('input[name=estimate_d7]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d8"){
                        if(val=="true"){
                            $('input[name=estimate_d8]').attr('checked', true);
                            $('input[name=estimate_d8]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d8]').attr('checked', false);
                            $('input[name=estimate_d8]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d9"){
                        if(val=="true"){
                            $('input[name=estimate_d9]').attr('checked', true);
                            $('input[name=estimate_d9]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d9]').attr('checked', false);
                            $('input[name=estimate_d9]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d10"){
                        if(val=="true"){
                            $('input[name=estimate_d10]').attr('checked', true);
                            $('input[name=estimate_d10]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d10]').attr('checked', false);
                            $('input[name=estimate_d10]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d11"){
                        if(val=="true"){
                            $('input[name=estimate_d11]').attr('checked', true);
                            $('input[name=estimate_d11]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d11]').attr('checked', false);
                            $('input[name=estimate_d11]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_d12"){
                        if(val=="true"){
                            $('input[name=estimate_d12]').attr('checked', true);
                            $('input[name=estimate_d12]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=estimate_d12]').attr('checked', false);
                            $('input[name=estimate_d12]').css('background-color','red');
                        }
                    }
                    else if(key=="estimate_reason"){$('#estimate_reason').html(val);}

                    //Management Data
                    else if(key=="im_IVfluidYN"){
                        if(val=="true"){
                            $('input[name=im_IVfluidYN]').attr('checked', true);
                            $('input[name=im_IVfluidYN]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=im_IVfluidYN]').attr('checked', false);
                            $('input[name=im_IVfluidYN]').css('background-color','red');
                        }
                    }
                    else if(key=="im_O2CanulaYN"){
                        if(val=="true"){
                            $('input[name=im_O2CanulaYN]').attr('checked', true);
                            $('input[name=im_O2CanulaYN]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=im_O2CanulaYN]').attr('checked', false);
                            $('input[name=im_O2CanulaYN]').css('background-color','red');
                        }
                    }
                    else if(key=="im_IntubationYN"){
                        if(val=="true"){
                            $('input[name=im_IntubationYN]').attr('checked', true);
                            $('input[name=im_IntubationYN]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=im_IntubationYN]').attr('checked', false);
                            $('input[name=im_IntubationYN]').css('background-color','red');
                        }
                    }
                    else if(key=="im_OtherYn"){
                        if(val=="true"){
                            $('input[name=im_OtherYn]').attr('checked', true);
                            $('input[name=im_OtherYn]').css('background-color','green');
                        }else if(val=="false"){
                            $('input[name=im_OtherYn]').attr('checked', false);
                            $('input[name=im_OtherYn]').css('background-color','red');
                        }
                    }
                    else if(key=="im_OtherTxt"){$('#im_OtherTxt').html(val);}


                });
                $("div#nihss_score").html(total);
                
            }
        });
        return false;
    }

});